package edu.uestc.canary.admin.web.controller;

import edu.uestc.canary.entity.dto.Result;
import edu.uestc.canary.entity.pojo.ShopType;
import edu.uestc.canary.service.ShopTypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 * 商铺类别api
 * REST-api
 *      post: http://localhost:8848/api/shop-types  新增商铺类别信息
 *      get:  http://localhost:8848/api/shop-types  获取全部商铺类别信息
 *      get:  http://localhost:8848/api/shop-types/:id  根据id获取商铺类别信息
 *      put:  http://localhost:8848/api/shop-types  根据id修改商铺类别信息
 *      delete: http://localhost:8848/api/shop-types/:id  根据id删除商铺类别信息
 */
@RestController
@RequestMapping("/shop-types")
@Tag(name = "商铺类别")
public class ShopTypeController {
    @Resource private ShopTypeService service;

    @PostMapping
    @Operation(summary = "添加商铺类别")
    public Result save(@RequestBody ShopType shopType){
        return service.save(shopType) ? Result.ok() : Result.fail();
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取商铺类别",description = "根据id获取商铺类别信息")
    @Parameter(name = "shopTypeId",description = "商铺类别id",in = ParameterIn.PATH)
    public Result findById(@PathVariable Integer id){
        var type = service.getById(id);
        return type != null ? Result.ok(type) : Result.fail(null,"商铺类别不存在");
    }

    @Operation(summary = "获取所有的商铺类别信息")
    @GetMapping
    public Result loadAll(){
        var types = service.list();
        return Result.ok(types);
    }

    @Operation(summary = "更新商铺类别", description = "根据id更新商铺类别")
    @PutMapping
    public Result updateById(@RequestBody ShopType shopType){
        return service.updateById(shopType) ? Result.ok() : Result.fail();
    }

    @Operation(summary = "删除商铺类别", description = "根据id删除商铺类别")
    @DeleteMapping("/{id}")
    @Parameter(name = "shopTypeId",description = "商铺类别id",in = ParameterIn.PATH)
    public Result removeById(@PathVariable Integer id){
        return service.removeById(id) ? Result.ok() : Result.fail(null,"商铺类别不存在");
    }
}
